# **********************************************************************
# *
# * PostGIS - Spatial Types for PostgreSQL
# * http://postgis.net
# *
# * Copyright (C) 2010-2022 Sandro Santilli <strk@kbt.io>
# * Copyright (C) 2008 Mark Cave-Ayland
# * Copyright (C) 2005 Refractions Research Inc.
# *
# * This is free software; you can redistribute and/or modify it under
# * the terms of the GNU General Public Licence. See the COPYING file.
# *
# **********************************************************************

#
# PostGIS PGXS build system
#

POSTGIS_PGSQL_VERSION=@POSTGIS_PGSQL_VERSION@

MODULE_big=postgis_topology-@POSTGIS_MAJOR_VERSION@
MODULEDIR=contrib/postgis-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
MODULEPATH=$$libdir/postgis_topology-@POSTGIS_MAJOR_VERSION@

ifeq (@LIBINCLUDEMINORVERSION@,yes)
MODULEPATH=$$libdir/postgis_topology-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
MODULE_big=postgis_topology-@POSTGIS_MAJOR_VERSION@.@POSTGIS_MINOR_VERSION@
endif

# Files to be copied to the contrib/ directory
DATA_built=topology.sql topology_upgrade.sql uninstall_topology.sql


# SQL preprocessor
SQLPP = @SQLPP@

# SQL objects (files requiring pre-processing)
SQL_OBJS = \
  topology.sql \
  topology_upgrade.sql \
  topology_before_upgrade.sql \
  topology_after_upgrade.sql

# Objects to build using PGXS

OBJS = postgis_topology.o

# Libraries to link into the module (proj, geos)
#
# Note: we specify liblwgeom.a directly in SHLIB_LINK rather than using
# -L... -l options to prevent issues with some platforms trying to link
# to an existing liblwgeom.so in the PostgreSQL $libdir supplied by an
# older version of PostGIS, rather than with the static liblwgeom.a
# supplied with newer versions of PostGIS
PG_CPPFLAGS += -I@top_builddir@/liblwgeom -I@top_srcdir@/liblwgeom -I@top_srcdir@/libpgcommon @CFLAGS@ @CPPFLAGS@ @PICFLAGS@
SHLIB_LINK_F = @top_builddir@/libpgcommon/libpgcommon.a @top_builddir@/liblwgeom/.libs/liblwgeom.a @SHLIB_LINK@

# Add SFCGAL Flags if defined
ifeq (@SFCGAL@,sfcgal)
PG_CPPFLAGS += @SFCGAL_CPPFLAGS@
SHLIB_LINK_F += @SFCGAL_LDFLAGS@
endif


# Extra files to remove during 'make clean'
EXTRA_CLEAN=$(SQL_OBJS) topology_upgrade.sql.in

# PGXS information
PG_CONFIG := @PG_CONFIG@
PGXS := @PGXS@
# NO_TEMP_INSTALL is a workaround for a 9.5dev bug. See:
# http://www.postgresql.org/message-id/CAB7nPqTsR5o3g-fBi6jbsVdhfPiLFWQ_0cGU5=94Rv_8W3qvFA@mail.gmail.com
NO_TEMP_INSTALL=yes


top_builddir = @top_builddir@
VPATH := @srcdir@
INSTALL = @INSTALL@
LIBTOOL := @LIBTOOL@

include $(PGXS)

# Set prefix variables _after_ the include of PGXS
prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@

# Set PERL _after_ the include of PGXS
PERL=@PERL@

# This is to workaround a bug in PGXS 8.4 win32 link line,
# see http://trac.osgeo.org/postgis/ticket/1158#comment:57
SHLIB_LINK := $(SHLIB_LINK_F) $(SHLIB_LINK)

$(OBJS): ../liblwgeom/.libs/liblwgeom.a ../libpgcommon/libpgcommon.a ../postgis_config.h


# If REGRESS=1 passed as a parameter, change the default install paths
# so that no prefix is included. This allows us to relocate to a temporary
# directory for regression testing.
ifeq ($(REGRESS),1)
        bindir=/bin
        pkglibdir=/lib
        datadir=/share
        datamoduledir=contrib/postgis
endif



# Generate any .sql file from .sql.in files by running them through the SQL pre-processor
%.sql: %.sql.in
	$(SQLPP) -I@top_builddir@/postgis -I@top_srcdir@ $< > $@.tmp
	grep -v '^#' $@.tmp | \
	$(PERL) -lpe "s'MODULE_PATHNAME'\$(MODULEPATH)'g" > $@
	rm -f $@.tmp

topology_upgrade.sql.in:  topology.sql ../utils/create_upgrade.pl
	$(PERL) @top_srcdir@/utils/create_upgrade.pl $< > $@
	#$(PERL) -0777 -ne 's/^(CREATE|ALTER) (CAST|OPERATOR|TYPE|TABLE|SCHEMA|DOMAIN|TRIGGER).*?;//msg;print;' $< > $@

topology_upgrade.sql: ../postgis/common_before_upgrade.sql topology_before_upgrade.sql topology_upgrade.sql.in topology_after_upgrade.sql ../postgis/common_after_upgrade.sql
	echo "BEGIN;" > $@
	cat $^ >> $@
	echo "COMMIT;" >> $@

topology.sql: \
	sql/cleanup/RemoveUnusedPrimitives.sql.in \
	sql/sqlmm.sql.in \
	sql/populate.sql.in \
	sql/polygonize.sql.in \
	sql/export/gml.sql.in \
	sql/export/TopoJSON.sql.in \
	sql/query/getnodebypoint.sql.in \
	sql/query/getedgebypoint.sql.in \
	sql/query/getfacebypoint.sql.in \
	sql/query/GetRingEdges.sql.in \
	sql/query/GetNodeEdges.sql.in \
	sql/query/GetFaceContainingPoint.sql.in \
	sql/manage/AddTopoGeometryColumn.sql.in \
	sql/manage/CopyTopology.sql.in \
	sql/manage/CreateTopology.sql.in \
	sql/manage/FindLayer.sql.in \
	sql/manage/FindTopology.sql.in \
	sql/manage/FixCorruptTopoGeometryColumn.sql.in \
	sql/manage/ManageHelper.sql.in \
	sql/manage/MakeTopologyPrecise.sql.in \
	sql/manage/TotalTopologySize.sql.in \
	sql/manage/populate_topology_layer.sql.in \
	sql/manage/RenameTopology.sql.in \
	sql/manage/TopologySummary.sql.in \
	sql/manage/UpgradeTopology.sql.in \
	sql/manage/ValidateTopologyRelation.sql.in \
	sql/manage/ValidateTopologyPrecision.sql.in \
	sql/manage/ValidateTopology.sql.in \
	sql/topoelement/topoelement_agg.sql.in \
	sql/topogeometry/type.sql.in \
	sql/topogeometry/totopogeom.sql.in \
	sql/topogeometry/cleartopogeom.sql.in \
	sql/topogeometry/topogeom_edit.sql.in \
	sql/topogeometry/simplify.sql.in \
	sql/predicates.sql.in \
	../libpgcommon/sql/AddToSearchPath.sql.inc \
	../postgis/sqldefines.h \
	../postgis_revision.h

uninstall_topology.sql: topology.sql ../utils/create_uninstall.pl
	$(PERL) @top_srcdir@/utils/create_uninstall.pl $< $(POSTGIS_PGSQL_VERSION) > $@

check-unit:

check-regress: topology.sql
	$(MAKE) -C test $@

check: check-regress

clean: clean-subdirs

clean-subdirs:
	$(MAKE) -C test clean

distclean: clean
	rm -f Makefile test/Makefile

installdir:
	mkdir -p $(DESTDIR)$(bindir)

install: install-importer install-exporter

install-importer: loader/pgtopo_import | installdir
	$(LIBTOOL) --mode=install $(INSTALL) $^ "$(DESTDIR)$(bindir)/pgtopo_import"

install-exporter: loader/pgtopo_export | installdir
	$(LIBTOOL) --mode=install $(INSTALL) $^ "$(DESTDIR)$(bindir)/pgtopo_export"

uninstall: uninstall-importer uninstall-exporter

uninstall-importer:
	rm -f '$(DESTDIR)$(bindir)/pgtopo_import'

uninstall-exporter:
	rm -f '$(DESTDIR)$(bindir)/pgtopo_export'
